/**
 *  *2010-6-17 上午11:19:16  add 
 */
package com.base.myproject.client.HR.setting;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;

public class HR_title_Setting extends BaseJDBCPanel{
	
	private BaseComboBoxForm<BaseModelData> dep;
	private BaseStore<BusiObject> store = null;
	private BaseGrid<BusiObject> grid = null;
	private PagingToolBar toolBar = null;
	
	public HR_title_Setting(){
		init();
	}
	public void init(){
		initWeigetx();
		registerMainStore(store);
		registerStore(store);
		setdefault();
	}
	
	public void complete(){
		super.complete();
		getToolbarll().add(new Label("职位查找："));
		getToolbarll().add(cread_field());
	}
	
	private void initWeigetx(){
		toolBar = new PagingToolBar(16);
		ContentPanel panel = new ContentPanel();
		panel.add(createGrid());
		panel.setBottomComponent(toolBar);
		panel.setHeaderVisible(false);
		panel.setSize(1000, 500);
		add(panel);
	}
	
	private void setdefault() {
		store.addDefault("type", "hr_title");
	}

	private BaseStore<BusiObject> getStore(){
		if(store==null) {
			createStore();
		}
		store.setToolBar(toolBar);
		store.initcompleted();
		return store;
	}
	private BaseStore<BusiObject> createStore(){
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("job_name");
		type.getField("job_name").setType(String.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("persion_num");
		type.getField("persion_num").setType(Integer.class);
		type.addField("department");
		type.getField("department").setType(String.class);
		type.addField("traininger_num");
		type.getField("traininger_num").setType(Integer.class);
		type.addField("traininger_num");
		type.getField("traininger_num").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("job_table");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from job_table where type='hr_title'");
	//	info.setForcenolimit("true");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}
	
	private BaseGrid<BusiObject> createGrid(){
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
	
		ColumnConfig C2 = new ColumnConfig("code", "编码", 80);
		C2.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("job_name", "名称", 180);
		C3.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C3);
	
		ColumnConfig C5 = new ColumnConfig("department", "部门", 120);
		dep = new BaseComboBoxForm<BaseModelData>("id","name","systeminfo","where len(id)=(select max(len(id)) from systeminfo) ",true);
		C5.setEditor(dep.getCelEditor());
		C5.setRenderer(dep);
		columns.add(C5);
		
		ColumnConfig C4 = new ColumnConfig("persion_num", "预算人数", 80);
		C4.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C4);
		
		ColumnConfig C6 = new ColumnConfig("traininger_num", "额定培训生人数", 100);
		C6.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C6);
		
		ColumnConfig C7 = new ColumnConfig("employerlevel", "默认雇佣级别", 100);
		BaseComboBoxX employerlevel = new BaseComboBoxX("name","dictionary","where type = 'zy'");
		C7.setEditor(employerlevel.getCellEditor());
		columns.add(C7);
	
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(476);
		grid.setWidth(1000);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.getStore().reload();
		return grid ;
	}
	
	public StoreFilterField<BusiObject> cread_field() {
		StoreFilterField<BusiObject> field = new StoreFilterField<BusiObject>() {
			@Override
			protected boolean doSelect(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
				String name = record.get("job_name");
				if (name != null) {
					name = name.toLowerCase();
					if (name.indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}

			@Override
			protected void onFilter() {
				super.onFilter();
			}

		};
		field.setWidth(100);
		field.setToolTip("对职位名称进行查找");
		field.bind(store);
		return field;
	}
}